// using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class Blackhole_Skill_Controller : MonoBehaviour
{
    [SerializeField] private GameObject hotKeyPrefab;
    [SerializeField] private List<KeyCode> keyCodeList;

    public float maxSize = 15;// 大小
    public float growSpeed = 1;// 扩大速度
    public float shrinkSpeed = 2;// 缩小速度

    private bool canGrow = true;// 圈扩大
    private bool canShrink;// 圈缩小
    private bool canCreateHotKeys = true;// 可以创造热键
    private bool canAttack;// 是否可以攻击
    private bool playerCanDisapear = true;// 角色可以隐藏，防止连续按r角色不见了

    private int amountOfAttacks = 2;// 攻击次数
    private float cloneAttackCooldown = .3f;// 攻击冷却
    private float cloneAttackTimer;// 冷却计时
    private float blackholeTimer;// 黑洞持续时间

    private List<Transform> targets = new List<Transform>();// 敌人列表
    private List<GameObject> createdHotKey = new List<GameObject>();// 生成的热键列表

    public bool playerCanExitState { get; private set; }// 角色是否退出状态

    // 设置基础数值
    public void SetupBlackhole(float _maxSize, float _growSpeed, float _shrinkSpeed, int _amountOfAttacks, float _cloneAttackCooldown, float _blackholeDuration)
    {
        maxSize = _maxSize;//黑洞直径
        growSpeed = _growSpeed;//变大速度
        shrinkSpeed = _shrinkSpeed;//缩小速度
        amountOfAttacks = _amountOfAttacks;//攻击次数
        cloneAttackCooldown = _cloneAttackCooldown;//
        blackholeTimer = _blackholeDuration;// 黑洞持续时间

        if (SkillManager.instance.clone.crystalInseadOfClone)
        {
            playerCanDisapear = false;
        }
    }


    void Start()
    {

    }


    void Update()
    {
        cloneAttackTimer -= Time.deltaTime;
        blackholeTimer -= Time.deltaTime;

        if (blackholeTimer < 0)// 持续时间外
        {
            blackholeTimer = Mathf.Infinity;
            if (targets.Count > 0)
            {
                ReleaseCloneAttack();// 发动技能
            }
            else
            {
                FinishBlackHoleAbility();   //退出黑洞状态
            }
        }

        if (Input.GetKeyDown(KeyCode.R))// 按下R
        {
            ReleaseCloneAttack();// 发动技能
        }
        CloneAttackLogic(); // 使用克隆体攻击

        // 圈扩大
        if (canGrow && !canShrink)
        {
            transform.localScale = Vector2.Lerp(transform.localScale, new Vector2(maxSize, maxSize), growSpeed * Time.deltaTime);
        }
        // 圈缩小
        if (canShrink)
        {
            transform.localScale = Vector2.Lerp(transform.localScale, new Vector2(-1, -1), shrinkSpeed * Time.deltaTime);
            if (transform.localScale.x < 0)
            {
                Destroy(gameObject);
            }
        }
    }

    // 发动技能
    private void ReleaseCloneAttack()
    {
        if (targets.Count <= 0) return;
        DestroyHotKey();// 摧毁热键标识
        canAttack = true;// 可以攻击
        canCreateHotKeys = false;// 不能生成热键
        if (playerCanDisapear)
        {
            playerCanDisapear = false;
            PlayerManager.instance.player.fx.MakeTransprent(true);// 隐藏角色
        }
    }

    // 使用克隆体攻击
    private void CloneAttackLogic()
    {
        if (cloneAttackTimer < 0 && canAttack)//&& amountOfAttacks>0
        {
            cloneAttackTimer = cloneAttackCooldown;//进入冷却，防止幻影出现太快

            int randonIndex = Random.Range(0, targets.Count);
            float xOffset;

            if (Random.Range(0, 100) > 50)
                xOffset = 2;
            else
                xOffset = -2;

            if (SkillManager.instance.clone.crystalInseadOfClone)//如果水晶替换幻影开启
            {
                SkillManager.instance.crystal.CreateCrystal();//创造水晶
                SkillManager.instance.crystal.CurrentCrtstalChooseRandomTarget();//随机选敌人
            }
            else
            {   //否则制作幻影随机攻击
                SkillManager.instance.clone.CreateClone(targets[randonIndex], new Vector3(xOffset, 0));
            }

            amountOfAttacks--;
            if (amountOfAttacks <= 0)//攻击结束
            {
                canAttack = false;//放在这里就不需要 amountOfAttacks>0
                Invoke("FinishBlackHoleAbility", 1f);
            }
        }
    }

    //退出黑洞状态
    private void FinishBlackHoleAbility()
    {
        DestroyHotKey();
        playerCanExitState = true;
        canShrink = true;
    }

    // 摧毁热键标识
    private void DestroyHotKey()
    {
        if (createdHotKey.Count <= 0)
        {
            return;
        }
        for (int i = 0; i < createdHotKey.Count; i++)
        {
            Destroy(createdHotKey[i]);
        }
    }

    // 圈对敌人的碰撞
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.GetComponent<Enemy>() != null)
        {
            other.GetComponent<Enemy>().FreezeTime(true);//冻结敌人
            CreateHotKey(other);
        }
    }

    private void OnTriggerExit2D(Collider2D other)
    {
        if (other.GetComponent<Enemy>() != null)
        {
            other.GetComponent<Enemy>().FreezeTime(false);
        }
    }
    //   private void OnTriggerExit2D(Collider2D other) =>other.GetComponent<Enemy>()?.FreezeTime(false);

    // 创建热键
    private void CreateHotKey(Collider2D other)
    {
        if (keyCodeList.Count <= 0)
            return;
        if (!canCreateHotKeys) return;
        // 创造热键放在敌人上方
        GameObject newHotKey = Instantiate(hotKeyPrefab, other.transform.position + new Vector3(0, 2), Quaternion.identity);
        createdHotKey.Add(newHotKey);
        // 随机取一个按键
        KeyCode chooseKey = keyCodeList[Random.Range(0, keyCodeList.Count)];
        keyCodeList.Remove(chooseKey);// 从按键列表移除选中的按键
        // 获取热键脚本
        Blackhole_HotKey_Skill_Controller newHotKeyScript = newHotKey.GetComponent<Blackhole_HotKey_Skill_Controller>();
        newHotKeyScript.SetupHotKey(chooseKey, other.transform, this);// 设置热键
    }
    // 添加敌人列表
    public void AddEnemyToList(Transform _enemyTransform) => targets.Add(_enemyTransform);
}
